package com.jeasonzhao.commons.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexHelper
{
    private RegexHelper()
    {
        super();
    }

    public static boolean matches(String strRegex,String strInput)
    {
        if(null == strRegex || null == strInput)
        {
            return false;
        }
        else
        {
            return java.util.regex.Pattern.matches(strRegex,strInput);
        }
    }

    public static boolean matchesIgnoreCase(String strRegex,String strInput)
    {
        if(null == strRegex || null == strInput)
        {
            return false;
        }
        else
        {

            Pattern p = Pattern.compile(strRegex,Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(strInput);
            return m.find();
        }
    }

    public static Matcher getMatcher(String strRegex,String strInput)
    {
        if(null == strRegex || null == strInput)
        {
            return null;
        }
        else
        {
            Pattern p = Pattern.compile(strRegex);
            return p.matcher(strInput);
        }
    }

    public static Matcher getMatcherIgnoreCase(String strRegex,String strInput)
    {
        if(null == strRegex || null == strInput)
        {
            return null;
        }
        else
        {
            Pattern p = Pattern.compile(strRegex,Pattern.CASE_INSENSITIVE);
            return p.matcher(strInput);
        }
    }

    public static String replaceAll(String strInitString,String strRegex,String strReplaced)
    {
        if(null == strRegex || null == strInitString || null == strReplaced)
        {
            return strInitString;
        }
        else
        {
            Pattern p = Pattern.compile(strRegex);
            return p.matcher(strInitString).replaceAll(strReplaced);
        }
    }

    public static String replaceAllIgnoreCase(String strInitString,String strRegex,String strReplaced)
    {
        if(null == strRegex || null == strInitString || null == strReplaced)
        {
            return strInitString;
        }
        else
        {
            Pattern p = Pattern.compile(strRegex,Pattern.CASE_INSENSITIVE);
            return p.matcher(strInitString).replaceAll(strReplaced);
        }
    }

    public static String getFirstMatchedGroup(String strRegex,String strInput)
    {
        if(null == strRegex || null == strInput)
        {
            return null;
        }
        else
        {
            return getMatchedGroup(strRegex,strInput,1);
        }
    }

    public static String getMatchedGroup(String strRegex,String strInput)
    {
        if(null == strRegex || null == strInput)
        {
            return null;
        }
        else
        {
            return getMatchedGroup(strRegex,strInput,0);
        }
    }

    public static String getFirstMatchedGroupIgnoreCase(String strRegex,String strInput)
    {
        if(null == strRegex || null == strInput)
        {
            return null;
        }
        else
        {
            return getMatchedGroupIgnoreCase(strRegex,strInput,1);
        }
    }

    public static String getMatchedGroupIgnoreCase(String strRegex,String strInput)
    {
        if(null == strRegex || null == strInput)
        {
            return null;
        }
        else
        {
            return getMatchedGroupIgnoreCase(strRegex,strInput,0);
        }
    }

    public static String getMatchedGroup(String strRegex,String strInput,int nGroup)
    {
        if(null == strRegex || null == strInput)
        {
            return null;
        }
        else
        {
            nGroup = nGroup < 0 ? 0 : nGroup;
            Pattern p = Pattern.compile(strRegex);
            Matcher m = p.matcher(strInput);
            if(m.find())
            {
                if(m.groupCount() >= nGroup)
                {
                    return m.group(nGroup);
                }
            }
            return null;
        }
    }

    public static String getMatchedGroupIgnoreCase(String strRegex,String strInput,int nGroup)
    {
        if(null == strRegex || null == strInput)
        {
            return null;
        }
        else
        {
            nGroup = nGroup < 0 ? 0 : nGroup;
            Pattern p = Pattern.compile(strRegex,Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(strInput);
            if(m.find())
            {
                if(m.groupCount() >= nGroup)
                {
                    return m.group(nGroup);
                }
            }
            return null;
        }
    }

    /**
     * @todo Known bug: do not convert '^' and '$' to normal characters.
     * @param str String
     * @return String
     */
    public static String wildcard2Regex(String str)
    {
        return null == str ? null :
            str
            .replaceAll("\\\\","###")
            .replaceAll("###","\\\\\\\\")
            .replaceAll("\\.","\\\\.")
            .replaceAll("\\*",".*")
            .replaceAll("\\?",".");
    }

    public static boolean wildcard(String strRegex,String strInput)
    {
        if(null == strRegex || null == strInput)
        {
            return false;
        }
        else
        {
            return matches(wildcard2Regex(strRegex),strInput);
        }
    }

    public static boolean wildcardIgnoreCase(String strRegex,String strInput)
    {
        return matchesIgnoreCase(wildcard2Regex(strRegex),strInput);
    }
}
